#ifndef _COMMAND_H
#define _COMMAND_H

#include "stm32f10x.h"
#include "cmd.h"
#include "UART.h"
#include "stdtypes.h"

#define  MAX_LENGTH_R         64
#define  MAX_LENGTH_S         64

#define  FRAME_HEAD_INDEX     0
#define  FRAME_ADDRESS_INDEX  1
#define  FRAME_LENGTH_INDEX   2
#define  FRAME_COMMAND_INDEX  3
#define  FRAME_DATA_INDEX     4 

#define BAD_COMMAND			 		0x0001		
#define BAD_COMMAND_LENGTH			0x0004		
#define BAD_FRAME_CHECKSUM			0x0080		
#define BAD_COMMAND_UNKNOWN			0x00FF	

extern u16* g_pReceiveBuffer ;
extern u16* g_pSentBuffer ;
extern u8* g_pSentBufferU8 ;

extern u16  g_ushReceiveBuffer[MAX_LENGTH_R] ; 
extern u16  g_ushSentBuffer[MAX_LENGTH_S]; 
extern u16  g_ushReceiveNum; 
extern u16  g_ushSentNum;

extern u8   SendSum ;
/* Function define */
extern u16 CommandParse(void);               //command parse

#define UART_R_CMD								g_pReceiveBuffer[FRAME_COMMAND_INDEX]
#define UART_R_DATA_P							(&g_pReceiveBuffer[FRAME_DATA_INDEX])
#define UART_R_DATAn_P(word)			       (&g_pReceiveBuffer[FRAME_DATA_INDEX+(word)])
#define UART_R_LENGTH							g_pReceiveBuffer[FRAME_LENGTH_INDEX]
#define UART_R_DATAn(idx)                       g_pReceiveBuffer[FRAME_DATA_INDEX+(idx)]

#define UART_S_CMD			      	g_pSentBuffer[FRAME_COMMAND_INDEX]
#define UART_S_DATA_P				(&g_pSentBuffer[FRAME_DATA_INDEX])
#define UART_S_LENGTH				g_pSentBuffer[FRAME_LENGTH_INDEX]


#define CMD_CMD1							0x1080		
#define CMD_CMD1_BYTE  						byteof(U32)
	
#define CMD_CMD2							0x1082		
#define CMD_CMD2_BYTE  						byteof(U32)

//MOTOR
#define CMD_DC_MOTOR_MOVE                   0x2000
#define CMD_DC_MOTOR_MOVE_BYTE              byteof(DC_MOTOR_ANALYSIS)

#define CMD_DC_MOTOR_STOP                   0x2002
#define CMD_DC_MOTOR_STOP_BYTE              byteof(DC_MOTOR_ANALYSIS)

#define CMD_Z_MOTOR_MOVE                    0x2004
#define CMD_Z_MOTOR_MOVE_BYTE               byteof(Z_MOTOR_MOVE_ANALYSIS)

#define CMD_Z_MOTOR_MOVETO                  0x2006
#define CMD_Z_MOTOR_MOVETO_BYTE             byteof(Z_MOTOR_MOVETO_ANALYSIS)

#define CMD_Z_MOTOR_STOP                    0x2008
#define CMD_Z_MOTOR_STOP_BYTE               byteof(Z_MOTOR_STOP_ANALYSIS)

#define CMD_Z_MOTOR_FINDZERO                0x200a
#define CMD_Z_MOTOR_FINDZERO_BYTE

#define CMD_Z_MOTOR_EN                      0x200c
#define CMD_Z_MOTOR_EN_BYTE 

//beep
#define CMD_BEEP                            0x2100
#define CMD_BEEP_BYTE

//EDM
#define CMD_EDM_SWITCH                      0x2200
#define CMD_EDM_SWITCH_BYTE                 byteof(HV_SWITCH_ANALY)

#define CMD_EDM_PWM_RUN                     0x2202
#define CMD_EDM_PWM_RUN_BYTE                byteof(ANALY_PWM_EDM)

#define CMD_EDM_PWM_STOP                    0x2204
#define CMD_EDM_PWM_STOP_BYTE               byteof(ANALY_PWM_EDM)

#define CMD_EDM_RC_RUN                      0x2206
#define CMD_EDM_RC_RUN_BYTE                 byteof(ANALY_RC_EDM)

#define CMD_EDM_RC_STOP                     0x2208
#define CMD_EDM_RC_STOP_BYTE                byteof(ANALY_RC_EDM)

//config
#define CMD_UPDATE_CONFIG                   0x2300
#define CMD_UPDATE_CONFIG_BYTE             

#endif /* _COMMAND_H */
